﻿#region Copyright (c) 2000-2013 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{                                                                   }
{                                                                   }
{       Copyright (c) 2000-2013 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2013 Developer Express Inc.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UColor.Core.Helpers;
using System.Windows.Input;
using System.Windows.Data;
namespace UColor.WPF.Internal {
	public sealed class BaseControlPagesContainer : BaseControlOrdinarPart {
		WeakEventHandler<ThePropertyChangedEventArgs<BaseControlPage>> onDemoBaseControlStatePageChanged;
		
		WeakEventHandler<ModuleAppearEventArgs> onMainPageModuleAppear;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onDemoBaseControlStateShowBackButtonChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onMainPageLoadingInProgressChanged;
		WeakEventHandler<DepPropertyChangedEventArgs> onDemoBaseControlAllowRunAnotherDemoChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onDemoBaseControlStateShowBuyNowButtonChanged;
		BaseControlPart currentPage;
		//LString copyright;
		string animationDirection;
		
		BaseControlModulesPage modulesPage;
		BaseControlMainPage mainPage;
		bool showBackButton;
		bool showBuyNowButton;
		bool actualShowBackButton;
		bool loadingInProgress;
		bool allowRunAnotherDemo;
		bool allowUseBackButton;
		public BaseControlPagesContainer(BaseControl demoBaseControl)
			: base(demoBaseControl) {
			onDemoBaseControlStatePageChanged = new WeakEventHandler<ThePropertyChangedEventArgs<BaseControlPage>>(OnDemoBaseControlStatePageChanged);
			
			onDemoBaseControlStateShowBackButtonChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnDemoBaseControlStateShowBackButtonChanged);
			onDemoBaseControlAllowRunAnotherDemoChanged = new WeakEventHandler<DepPropertyChangedEventArgs>(OnDemoBaseControlAllowRunAnotherDemoChanged);
			onDemoBaseControlStateShowBuyNowButtonChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnDemoBaseControlStateShowBuyNowButtonChanged);
			DemoBaseControl.AllowRunAnotherDemoChanged += onDemoBaseControlAllowRunAnotherDemoChanged.Handler;
			DemoBaseControl.State.ModuleIDChanged += OnModuleIDChanged;
			OnDemoBaseControlAllowRunAnotherDemoChanged(DemoBaseControl, new DepPropertyChangedEventArgs(BaseControl.AllowRunAnotherDemoProperty, false, DemoBaseControl.AllowRunAnotherDemo));
			Initialized();
		}
		void OnModuleIDChanged(object sender, ThePropertyChangedEventArgs<object> e) {
			AllowUseBackButton = false;
		}
		public BaseControlPart CurrentPage {
			get { return currentPage; }
			private set { SetValue<BaseControlPart>("CurrentPage", ref currentPage, value); }
		}
		
		public string AnimationDirection {
			get { return animationDirection; }
			private set { SetValue<string>("AnimationDirection", ref animationDirection, value); }
		}
		public bool AllowRunAnotherDemo {
			get { return allowRunAnotherDemo; }
			set { SetValue<bool>("AllowRunAnotherDemo", ref allowRunAnotherDemo, value, RaiseAllowRunAnotherDemoChanged); }
		}
		public bool ShowBuyNowButton {
			get { return showBuyNowButton; }
			set { SetValue<bool>("ShowBuyNowButton", ref showBuyNowButton, value); }
		}
		public bool AllowUseBackButton {
			get { return allowUseBackButton; }
			set { SetValue<bool>("AllowUseBackButton", ref allowUseBackButton, value); }
		}
		public event EventHandler<ModuleAppearEventArgs> ModuleAppear;
		public event EventHandler BackButtonClick;
		public event EventHandler BuyButtonClick;
		public event ThePropertyChangedEventHandler<BaseControlPage> ActualPageChanged;
		
		public void Back() {
            mainPage.ModuleWrapper.ClearModuleList();
			if(BackButtonClick != null)
				BackButtonClick(this, EventArgs.Empty);
		}
		public void BuyNow() {
			if(BuyButtonClick != null)
				BuyButtonClick(this, EventArgs.Empty);
		}
		
		public BaseControlModulesPage ModulesPage {
			get {
				if(modulesPage == null) {
					modulesPage = new BaseControlModulesPage(this,DemoBaseControl);
					
				}
				return modulesPage;
			}
		}
		
		public BaseControlMainPage MainPage {
			get {
				if(mainPage == null) {
					mainPage = new BaseControlMainPage(DemoBaseControl);
					onMainPageModuleAppear = new WeakEventHandler<ModuleAppearEventArgs>(OnMainPageModuleAppear);
					onMainPageLoadingInProgressChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnMainPageLoadingInProgressChanged);
					mainPage.ModuleAppear += onMainPageModuleAppear.Handler;
					mainPage.LoadingInProgressChanged += onMainPageLoadingInProgressChanged.Handler;
				}
				return mainPage;
			}
		}
		public bool ShowBackButton {
			get { return showBackButton; }
			set { SetValue<bool>("ShowBackButton", ref showBackButton, value, RaiseShowBackButtonChanged); }
		}
		public bool ActualShowBackButton {
			get { return actualShowBackButton; }
			private set { SetValue<bool>("ActualShowBackButton", ref actualShowBackButton, value); }
		}
		public bool LoadingInProgress {
			get { return loadingInProgress; }
			private set { SetValue<bool>("LoadingInProgress", ref loadingInProgress, value); }
		}
		protected override void OnDemoBaseControlStateChanged(BaseControlState oldValue, BaseControlState newValue) {
			base.OnDemoBaseControlStateChanged(oldValue, newValue);
			if(oldValue != null) {
				oldValue.PageChanged -= onDemoBaseControlStatePageChanged.Handler;
				oldValue.ShowBackButtonChanged -= onDemoBaseControlStateShowBackButtonChanged.Handler;
				newValue.ShowBuyNowButtonChanged -= onDemoBaseControlStateShowBuyNowButtonChanged.Handler;
			}
			if(newValue != null) {
				newValue.PageChanged += onDemoBaseControlStatePageChanged.Handler;
				OnDemoBaseControlStatePageChanged(newValue, new ThePropertyChangedEventArgs<BaseControlPage>(oldValue == null ? BaseControlPage.None : oldValue.Page, newValue.Page));
				newValue.ShowBackButtonChanged += onDemoBaseControlStateShowBackButtonChanged.Handler;
				OnDemoBaseControlStateShowBackButtonChanged(newValue, new ThePropertyChangedEventArgs<bool>(false, newValue.ShowBackButton));
				newValue.ShowBuyNowButtonChanged += onDemoBaseControlStateShowBuyNowButtonChanged.Handler;
				OnDemoBaseControlStateShowBuyNowButtonChanged(newValue, new ThePropertyChangedEventArgs<bool>(false, newValue.ShowBuyNowButton));
			}
		}
		void OnDemoBaseControlAllowRunAnotherDemoChanged(object sender, DepPropertyChangedEventArgs e) {
			AllowRunAnotherDemo = (bool)e.NewValue;
		}
		void OnDemoBaseControlStateShowBackButtonChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			ShowBackButton = e.NewValue;
		}
		void OnDemoBaseControlStateShowBuyNowButtonChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			ShowBuyNowButton = e.NewValue;
		}
		void RaiseShowBackButtonChanged(bool oldValue, bool newValue) {
			UpdateActualShowBackButton();
		}
		void RaiseAllowRunAnotherDemoChanged(bool oldValue, bool newValue) {
			UpdateActualShowBackButton();
		}
		void OnDemoBaseControlStatePageChanged(object sender, ThePropertyChangedEventArgs<BaseControlPage> e) {
			AnimationDirection = GetAnimationDuration(e.OldValue, e.NewValue);
			AllowUseBackButton = false;
			switch(e.NewValue) {
				case BaseControlPage.None: 
					CurrentPage = null; 
					break;
				
				case BaseControlPage.ModulesPage: 
					CurrentPage = ModulesPage;
					break;
				default: 
					CurrentPage = MainPage;
					break;
			}
			UpdateActualShowBackButton();
		}
		void UpdateActualShowBackButton() {
			ActualShowBackButton = ShowBackButton && (allowRunAnotherDemo || DemoBaseControl.State == null || DemoBaseControl.State.Page == BaseControlPage.MainPage);
		}
		
		void OnMainPageLoadingInProgressChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			LoadingInProgress = e.NewValue && CurrentPage != MainPage;
		}
		
		string GetAnimationDuration(BaseControlPage oldPage, BaseControlPage newPage) {
			if(oldPage == BaseControlPage.None ||  newPage == BaseControlPage.MainPage) return "Forward";
			return "Back";
		}
		void OnMainPageModuleAppear(object sender, ModuleAppearEventArgs e) {
			AllowUseBackButton = true;
			if(ModuleAppear != null)
				ModuleAppear(this, e);
		}
		public void RaiseActualPageChanged(object pageView) {
			BaseControlPage actualPage = BaseControlPage.None;
			if(pageView == null)
				actualPage = BaseControlPage.None;
			
			else if(pageView == ModulesPage.View)
				actualPage = BaseControlPage.ModulesPage;
			else if(pageView == MainPage.View)
				actualPage = BaseControlPage.MainPage;
			AllowUseBackButton = true;
			if(ActualPageChanged != null)
				ActualPageChanged(this, new ThePropertyChangedEventArgs<BaseControlPage>(BaseControlPage.None, actualPage));
		}
		#region Commands
		protected override void InitializeCommands() {
			base.InitializeCommands();
			BackCommand = new SimpleActionCommand(DoBack);
			BuyNowCommand = new SimpleActionCommand(DoBuyNow);
			RaiseActualPageChangedCommand = new SimpleActionCommand(DoRaiseActualPageChanged);
		}
		public ICommand BuyNowCommand { get; private set; }
		public ICommand BackCommand { get; private set; }
		public ICommand RaiseActualPageChangedCommand { get; private set; }
		void DoBack(object p) { Back(); }
		void DoBuyNow(object p) { BuyNow(); }
		void DoRaiseActualPageChanged(object page) { RaiseActualPageChanged(page); }
		#endregion
	}
}
